﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using Microsoft.Xna.Framework;
using Microsoft.Xna.Framework.Graphics;
using Microsoft.Xna.Framework.Input;

namespace MenuGame2DXNA
{
    public class sChoosePlayer : Scene
    {
        
        private VisibleEntityManager veManager;
        private Game game;
        private GroupLayout listPlayer;
        private Dialog dialog;
        KeyboardState oldState;
             private int iSelected;
            
        public override void LoadContent(Game _game)
        {
           
            game = _game;

            base.LoadContent(game);
          
            listPlayer = new GroupLayout(game.Content, "LayoutChoosePlayer.xml", "");
            veManager = new VisibleEntityManager();
            
            listPlayer.UpdaterealPositon( new Vector2(200, 180));

            listPlayer.TopLeft = new Vector2(-900, -900);
            dialog = new Dialog(game,600,300,listPlayer,"CHOOSE PLAYER");
            
           
            veManager.AddObject(dialog);

             iSelected = 0;
            
             Stop();
            
        }


        public override void Update(GameTime gameTime)
        {
            if (!isPlayMusic)
            {
                MusicManager.Play("Title");
                isPlayMusic = true;
            }
            KeyboardState kbState = Keyboard.GetState();
            if (kbState.IsKeyDown(Keys.Escape))
            {
                game.Scene = 0;
                Stop();
            }
            if (kbState.IsKeyDown(Keys.Space))
            {
               
                    game.IDColor = iSelected;
                    game.Scene = 1;
                    Stop();
                

            }
            if (kbState.IsKeyDown(Keys.Right))
            {
                if (!oldState.IsKeyDown(Keys.Right))
                {
                    SoundManager.Play("item");
                    if (iSelected < listPlayer.getCount()-2)
                        iSelected++;
                    else
                        iSelected = 0;

                }

            }

            // kbState.
            if (kbState.IsKeyDown(Keys.Left))
            {

                if (!oldState.IsKeyDown(Keys.Left))
                {
                    SoundManager.Play("item");
                    if (iSelected > 0)
                        iSelected--;
                    else
                        iSelected = listPlayer.getCount()-2;
                }

            }

            oldState = kbState;
            
            for (int i = 0; i < listPlayer.getCount()-1; i++)
            {
                if (i == iSelected)
                {
                    listPlayer[i].Color = Color.GreenYellow;
                    listPlayer["arrow"].Position = new Vector2(listPlayer[i].Position.X + 20,
                        listPlayer[i].Position.Y - 40);
                }
                else
                    listPlayer[i].Color = Color.White;
            }
            veManager.Update(gameTime, Color.White);
        }


        public override void Draw(GameTime gameTime, SpriteBatch spriteBatch)
        {
            base.Draw(gameTime, spriteBatch);
            veManager.Draw(gameTime, spriteBatch);
        }
        public override void Play()
        {
            base.Play();
            dialog.Appear(54);
            oldState = new KeyboardState();
            
        }
        public override void Stop()
        {
            
            dialog.DisAppear(1);
            base.Stop();
        }

    }
}
